// pages/addFabric/addFabric.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: '',
    index: '',
    list: '',
    i: 'false',
    isTui: '',
    width: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    wx.setNavigationBarTitle({
      title: options.type
    })
    this.setData({
      type: options.type,
      i: options.i,
      isTui: options.isTui
    })
    if (options.index != undefined) {
      this.setData({
        index: options.index
      })
    }
    if (options.index != undefined && options.i == 'false') {
      console.log(1)
      this.setData({
        index: options.index,
        list: options.type == '面料' ? app.globalData.fabricList[options.index] : app.globalData.auxiliaryFabricList[options.index],
        width: options.type == '面料' ? app.globalData.fabricList[options.index].width : app.globalData.auxiliaryFabricList[options.index].width,
      })
    } else if (options.index != undefined && options.i == 'true') {
      this.setData({
        index: options.index,
        list: options.type == '面料' ? app.globalData.XfabricList[options.index] : app.globalData.XauxiliaryFabricList[options.index],
        width: options.type == '面料' ? app.globalData.XfabricList[options.index].width : app.globalData.XauxiliaryFabricList[options.index].width,
      })
      console.log(this.data.list)

    }
  },
  submit: function(e) {
    if (e.detail.value.name == '' || e.detail.value.number == '' || e.detail.value.width == '' || e.detail.value.unit == '' || e.detail.value.unitAmount == '' || e.detail.value.color == '' || e.detail.value.supplierName == '' || e.detail.value.contactMode == '') {
      wx.showToast({
        icon: 'none',
        title: '请填写完整信息',
      })
      return
    }
    console.log(e)
    if (this.data.type == '面料' && this.data.index == '' && this.data.i == 'false') {
      app.globalData.fabricList.push({
        fabricName: e.detail.value.name,
        supplierProductNumber: e.detail.value.number,
        width: e.detail.value.width,
        unit: e.detail.value.unit,
        unitAmount: e.detail.value.unitAmount,
        color: e.detail.value.color,
        fabricColor: e.detail.value.color,
        supplierName: e.detail.value.supplierName,
        contactMode: e.detail.value.contactMode,
        fabricType: this.data.type,
        id: ''
        // unitPrice: e.detail.value.unitPrice,
        // fabricPrice: e.detail.value.totalPrice
      })
    } else if (this.data.type == '辅料' && this.data.index == '' && this.data.i == 'false') {
      app.globalData.auxiliaryFabricList.push({
        fabricName: e.detail.value.name,
        supplierProductNumber: e.detail.value.number,
        width: e.detail.value.width,
        unit: e.detail.value.unit,
        unitAmount: e.detail.value.unitAmount,
        color: e.detail.value.color,
        fabricColor: e.detail.value.color,
        supplierName: e.detail.value.supplierName,
        contactMode: e.detail.value.contactMode,
        fabricType: this.data.type,
        id: ''
        // unitPrice: e.detail.value.unitPrice,
        // auxiliaryFabricPrice: e.detail.value.totalPrice
      })
    } else if (this.data.type == '面料' && this.data.index != '' && this.data.i == 'false') {
      app.globalData.fabricList[this.data.index] = {
        fabricName: e.detail.value.name,
        supplierProductNumber: e.detail.value.number,
        width: e.detail.value.width,
        unit: e.detail.value.unit,
        unitAmount: e.detail.value.unitAmount,
        color: e.detail.value.color,
        fabricColor: e.detail.value.color,
        supplierName: e.detail.value.supplierName,
        contactMode: e.detail.value.contactMode,
        fabricType: this.data.type,
        id: this.data.list.id
        // unitPrice: e.detail.value.unitPrice,
        // fabricPrice: e.detail.value.totalPrice
      }
    } else if (this.data.type == '辅料' && this.data.index != '' && this.data.i == 'false') {
      app.globalData.auxiliaryFabricList[this.data.index] = {
        fabricName: e.detail.value.name,
        supplierProductNumber: e.detail.value.number,
        width: e.detail.value.width,
        unit: e.detail.value.unit,
        unitAmount: e.detail.value.unitAmount,
        color: e.detail.value.color,
        fabricColor: e.detail.value.color,
        supplierName: e.detail.value.supplierName,
        contactMode: e.detail.value.contactMode,
        fabricType: this.data.type,
        id: this.data.list.id
        // unitPrice: e.detail.value.unitPrice,
        // auxiliaryFabricPrice: e.detail.value.totalPrice
      }
    } else if (this.data.type == '面料' && this.data.index != '' && this.data.i == 'true') {
      app.globalData.XfabricList[this.data.index].fabricName = e.detail.value.name;
      app.globalData.XfabricList[this.data.index].supplierProductNumber = e.detail.value.number;
      app.globalData.XfabricList[this.data.index].width = e.detail.value.width;
      app.globalData.XfabricList[this.data.index].unit = e.detail.value.unit;
      app.globalData.XfabricList[this.data.index].unitAmount = e.detail.value.unitAmount;
      app.globalData.XfabricList[this.data.index].color = e.detail.value.color;
      app.globalData.XfabricList[this.data.index].fabricColor = e.detail.value.color;
      app.globalData.XfabricList[this.data.index].contactMode = e.detail.value.contactMode;
      app.globalData.XfabricList[this.data.index].fabricType = this.data.type;
      // app.globalData.XfabricList[this.data.index].unitPrice = e.detail.value.unitPrice;
      // app.globalData.XfabricList[this.data.index].fabricPrice = e.detail.value.totalPrice;
    } else if (this.data.type == '辅料' && this.data.index != '' && this.data.i == 'true') {
      app.globalData.XauxiliaryFabricList[this.data.index].fabricName = e.detail.value.name;
      app.globalData.XauxiliaryFabricList[this.data.index].supplierProductNumber = e.detail.value.number;
      app.globalData.XauxiliaryFabricList[this.data.index].width = e.detail.value.width;
      app.globalData.XauxiliaryFabricList[this.data.index].unit = e.detail.value.unit;
      app.globalData.XauxiliaryFabricList[this.data.index].unitAmount = e.detail.value.unitAmount;
      app.globalData.XauxiliaryFabricList[this.data.index].color = e.detail.value.color;
      app.globalData.XauxiliaryFabricList[this.data.index].fabricColor = e.detail.value.color;
      app.globalData.XauxiliaryFabricList[this.data.index].contactMode = e.detail.value.contactMode;
      app.globalData.XauxiliaryFabricList[this.data.index].fabricType = this.data.type;
      // app.globalData.XauxiliaryFabricList[this.data.index].unitPrice = e.detail.value.unitPrice;
      // app.globalData.XauxiliaryFabricList[this.data.index].auxiliaryFabricPrice = e.detail.value.totalPrice;
    }
    wx.navigateBack({
      delta: 1
    })
    console.log(app.globalData.auxiliaryFabricList)
  },
  regExp: (str) => {
    var re = /[^0-9.]/g
    return str.replace(re, '')
  },
  width: function(e) {
    let width = this.regExp(e.detail.value)
    this.setData({
      width: width
    })
  },
  // unitPrice:function(e){
  //   let unitPrice = this.regExp(e.detail.value)
  //   this.setData({
  //     unitPrice: unitPrice
  //   })
  // },
  // price: function (e) {
  //   let price = this.regExp(e.detail.value)
  //   this.setData({
  //     price: price
  //   })
  // },
  remove() {
    var that = this
    let api = this.data.type == '面料' ? app.globalData.fabricList[this.data.index].id : app.globalData.auxiliaryFabricList[this.data.index].id
    app.req('designDiagram/madeFabric/' + api, '', 'DELETE', res => {
      console.log(res)
      if (res.data.code == 200) {
        if (that.data.type == '面料') {
          app.globalData.fabricList.splice(this.data.index, 1)
        } else {
          app.globalData.auxiliaryFabricList.splice(this.data.index, 1)
        }
        wx.navigateBack({
          delta: 1
        })
      }
    })
  }
})